﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace MVCLD.Models
{
    public class CategoryModels
    {
        #region Data Access
        MVCLDServiceModels db = new MVCLDServiceModels();
        #endregion

        public List<Category> GetSubCategories(List<Category> Cats) 
        {
            string count = "-";
            List<Category> MainCats = new List<Category>();
            foreach (var item in Cats) {
            // Add the first one and see if it has child categories
                if (item.MainCategory == true)
                    item.CategoryName = "[" + item.CategoryName + "]";
            MainCats.Add(item);
            var children = db.GetAllCategories().Where(c => c.ParentCategoryID == item.CategoryID);
            if (children.Count() > 0)
            {
                foreach (var subcats in GetSubCategories(children.ToList()))
                {
                    subcats.CategoryName = count + subcats.CategoryName;
                    MainCats.Add(subcats);
                    count += "-";
                }
            }
            count = "-";
            }
            return MainCats;
        }

    }

   [MetadataType(typeof(Category_Validation))]
    public partial class Category     
    {
       class Category_Validation 
       {

           [ScaffoldColumn(false)]
           public Guid CategoryID{get; set;}

           [Required(ErrorMessage = "Category name is required")]
           [StringLength(300)]
           [DisplayName("Cateogry Name")]
           public string CategoryName { get; set; }

           [DisplayName("Sort Order")]
           public int CategorySort { get; set; }

           [DisplayName("Is A Main Category")]
           public Boolean MainCategory { get; set; }

           [DisplayName("Parent Category")]
           [UIHint("CategoryDropDown")]
           public Guid ParentCategoryID { get; set; }

           [ScaffoldColumn(false)]
           public int UrlID { get; set; }
     
       }

    }


}
